<template>
  <div class='articles-container'>
    <el-card>
      <!-- 筛选 -->
     <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="关键字">
              <el-input style="200px" v-model="requestParams.keyword"></el-input>
           </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="requestParams.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clear()">清除</el-button>
              <el-button @click="filter()" type="primary">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
         <el-col :span="6" style="text-align:right">
          <el-button icon="el-icon-edit" @click="openDialog()" type="success" size="small">新增技巧</el-button>
        </el-col>
        </el-row>
      <!-- 提示 -->
        <el-alert
        :title="`数据一共 ${total}条`"
        type="info"
        style="margin-bottom:15px"
        class="alert"
        :closable="false"
        show-icon>
        </el-alert>
      <!-- 表格 -->
      <el-table :data="articles" stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="80px"></el-table-column>
        <el-table-column label="文章标题" width="400px">
          <template slot-scope="scope">
             {{ scope.row.title }}
           <a @click.prevent="openVideo(scope.row.videoURL)" v-if="scope.row.videoURL" href="#" class="el-icon-film"></a>
          </template>
        </el-table-column>
        <el-table-column prop="" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">{{scope.row.createTime|parseTimeByString}}</template>
        </el-table-column>
        <el-table-column  label="状态" width="100px">
          <template slot-scope="scope">{{scope.row.state===1?'已启用':'已警用'}}</template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
          <el-button type="text" @click="previewArticle(scope.row)">预览</el-button>
          <el-button type="text" @click="toggleState(scope.row)">{{scope.row.state===1?'禁用':'启用'}}</el-button>
          <el-button type="text" @click="openDialog(scope.row)" :disabled="scope.row.state === 1">修改</el-button>
          <el-button type="text" @click="delArticle(scope.row)"  :disabled="scope.row.state === 1||scope.row.totals > 0 ">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
       <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        @current-change="pager"
        :page-sizes="[5,10,20,50]"
        @size-change="handleSizeChange"
        hide-on-single-page
      ></el-pagination>
    </el-card>
   <articles-add ref="articlesAdd" :data="currArticle" @updateList="updateList()"></articles-add>
    <!-- 预览 -->
    <articles-preview ref="articlesPreview" :data="currArticle"></articles-preview>
    <!-- 视频 -->
    <div>
    <!-- 页头配置 -->
    <div class="video-preview" v-if="playerOptions.sources[0].src">
      <div class="close" @click="closeVideo()"><span class="el-icon-close"></span></div>
      <!--在视频外面加一个容器-->
      <video-player
        class="video-player vjs-custom-skin"
        ref="videoPlayer"
        :playsinline="true"
        :options="playerOptions"
      ></video-player>
    </div>
  </div>

  </div>

</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import ArticlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'
export default {
  name: 'ArticlesList',
  components: { ArticlesAdd, ArticlesPreview },
  data () {
    return {
      requestParams: {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      articles: [],
      currArticle: {},
      videoURL: null,
      srcvideoURL: '',
      // 视频播放
      playerOptions: {
        playbackRates: [0.5, 1.0, 1.5, 2.0], // 可选择的播放速度
        autoplay: false, // 如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [
          {
            type: 'video/mp4',
            src: ''
          }
        ],
        poster: '', // 你的封面地址
        // width: document.documentElement.clientWidth,
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: false, // 是否显示剩余时间功能
          fullscreenToggle: true // 全屏按钮
        }
      }
    }
  },
  watch: {
    videoURL (newval) {
      this.playerOptions.sources[0].src = newval
      this.srcvideoURL = newval
    }
  },
  created () {
    this.getList()
    this.playerOptions.sources[0].src = this.videoURL
  },
  methods: {
    // 视频
    openVideo (url) {
      this.videoURL = url
      this.playerOptions.sources[0].src = this.videoURL
    },
    closeVideo () {
      this.videoURL = null
    },
    // 预览
    previewArticle (article) {
      this.currArticle = article
      console.log(this.currArticle)
      console.log(this.$refs.articlesPreview)
      this.$nextTick(() => {
        this.$refs.articlesPreview.open()
      })
    },
    // 添加 编辑
    openDialog (article = {}) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesAdd.open()
      })
    },
    // 清除
    clear () {
      this.requestParams = {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 搜索
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    // 切换状态
    async toggleState (article) {
      await changeState({
        id: article.id,
        state: article.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      article.state = article.state === 1 ? 0 : 1
    },
    // 查询列表
    async getList () {
      const { data } = await list(this.requestParams)
      this.articles = data.items
      this.total = data.counts
    },
    // 条数
    handleSizeChange (size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.getList()
    },
    // 分页
    pager (np) {
      this.requestParams.page = np
      this.getList()
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currArticle.id) {
        this.requestParams.page = 1
      }
      this.getList()
    },
    // 删除
    async delArticle (article) {
      await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(article)
      this.$message.success('删除成功')
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.articles-container{
  padding: 10px;
  .el-icon-film{
    color: blue;
    font-size: 18px;
  }
}
.video-preview{
  position: fixed;
  left: 50%;
  top: 630px;
  width: 1000px;
  height: 1000px;
  transform: translate(-44%, -50%);
  // background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 9998;
  .close{
  width: 20px;
  height: 20px;
  position: absolute;
  top: 0;
  right:-10px;
  transform: translate(-50%,0);
  // background: rgba(0,0,0,0.4);
  // box-shadow: 0 0 5px rgba(0,0,0,0.4);
  border-radius: 50%;
  text-align: center;
  line-height: 50px;
  color: #fff;
    font-size: 20px;
    z-index: 9999;
    .el-icon-close::before{
    position: relative;
    top: -15px;
    right: 0;
    }
  }
  .box{
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
    width: 100%;
    height: 100%;
    }
  }
}
</style>
